import request from '@/utils/request';
import { getToken } from "@/utils/auth";

// 获取用户列表
export function getDataList(keyword, pageNum, recordNum, roleId = null, departmentId = null) {
  return request({
    url: process.env.VUE_APP_SERVICE_NAME + 'api/sysUser/getListByPage',
    method: 'post',
    data: {
      "operationId": getToken(),
      "queryStr": keyword,
      "pageNum": pageNum,
      "recordNum": recordNum
    }
  });
}

// 创建或者编辑用户
export function save(user) {
  let url = process.env.VUE_APP_SERVICE_NAME + 'api/sysUser/create';
  user.createUserId = getToken();
  user.modificationUserId = getToken();
  if (user.objectId || user.objectId == 0) {
    url = process.env.VUE_APP_SERVICE_NAME + 'api/sysUser/update';
  }
  return request({
    url: url,
    method: 'post',
    data: user
  });
}

// 删除选中用户数据
export function deleteSelectedData(ids) {
  return request({
    url: process.env.VUE_APP_SERVICE_NAME + 'api/sysUser/deleteByList',
    method: 'post',
    data: {
      operationId: getToken(),
      keyValues: ids
    }
  });
}

// 获取系统组织架构
export function getSystemStructureTree(isEnable = true) {
  return request({
    url: process.env.VUE_APP_SERVICE_NAME + 'api/sysStruct/getSysStructsTree',
    method: 'get',
    params: {
      pkSysStruct: 0,
      operationId: getToken(),
      isEnable: isEnable
    }
  });
}

// 删除选中部门
export function deleteSelectedDepartment(ids) {
  return request({
    url: process.env.VUE_APP_SERVICE_NAME + 'api/sysStruct/deleteByList',
    method: 'post',
    data: {
      "keyValues": ids,
      "operationId": getToken()
    }
  });
}

// 创建或者编辑部门
export function saveDepartment(department) {
  let url = process.env.VUE_APP_SERVICE_NAME + 'api/sysStruct/create';
  const postObject = {
    "createUserId": getToken(),
    "modificationUserId": getToken(),
    "pid": department.parentId,
    "name": department.text,
    "code": department.code,
    "isEnable": department.isEnable,
    "remark": department.remark,
    "isCheckCode": 0
  };
  if (department.id) {
    url = process.env.VUE_APP_SERVICE_NAME + 'api/sysStruct/update';
    postObject.objectId = department.id;
    postObject.createTime = department.createTime;
  }
  return request({
    url: url,
    method: 'post',
    data: postObject
  });
}

// 获取角色列表
export function getRoleList(keyword, pageNum, record_number) {
  return request({
    url: process.env.VUE_APP_SERVICE_NAME + 'api/sysRole/getListByPage',
    method: 'post',
    data: {
      "operationId": getToken(),
      "queryStr": keyword,
      "pageNum": pageNum,
      "recordNum": record_number
    }
  });
}

// 删除选中角色
export function deleteSelectedRole(ids) {
  return request({
    url: process.env.VUE_APP_SERVICE_NAME + 'api/sysRole/deleteByList',
    method: 'post',
    data: {
      "keyValues": ids,
      "operationId": getToken()
    }
  });
}

// 编辑角色信息
export function saveRole(role) {
  let url = process.env.VUE_APP_SERVICE_NAME + 'api/sysrole/update';
  if (role.type === 'create') {
    url = process.env.VUE_APP_SERVICE_NAME + 'api/sysrole/create';
  }
  return request({
    url: url,
    method: 'post',
    data: role
  });
}

export function getSearchUserList(keyword, pageNum, record_number = 20, roleId = null, departmentId = null) {
  return request({
    url: process.env.VUE_APP_SERVICE_NAME + 'api/sysUser/getListByPage',
    method: 'post',
    data: {
      "structId": departmentId,
      "roleId": roleId,
      "operationId": getToken(),
      "queryStr": keyword,
      "pageNum": pageNum,
      "recordNum": record_number
    }
  });
}

export function updateRoleUsers(allUsers, selectedUsers, roleId) {
  const postObject = {
    operationId: getToken(),
    roleId: roleId,
    dataChecks: []
  };
  allUsers.forEach(user => {
    let checkFlag = false;
    selectedUsers.forEach(item => {
      if (user.objectId === item.objectId) {
        checkFlag = true;
      }
    });
    postObject.dataChecks.push({
      objectId: user.objectId,
      checkFlag: checkFlag
    });
  });
  return request({
    url: process.env.VUE_APP_SERVICE_NAME + 'api/sysRole/updateRoleUsers',
    method: 'post',
    data: postObject
  });
}

export function getRolePermissions(menuId, roleId) {
  return request({
    url: process.env.VUE_APP_SERVICE_NAME + 'api/sysPermission/getListByPage',
    method: 'post',
    data: {
      menuCode: menuId && menuId != '' ? menuId : 'no-record',
      pageNum: 1,
      recordNum: 100,
      operationId: getToken(),
      roleId: roleId
    }
  });
}

export function updateRolePermissions(allCodes, selectedCodes, roleId, menuId) {
  const postObject = {
    operationId: getToken(),
    roleId: roleId,
    // menuId: menuId,
    dataChecks: []
  };
  allCodes.forEach(code => {
    let checkFlag = false;
    selectedCodes.forEach(item => {
      if (code.objectId === item.objectId) {
        checkFlag = true;
      }
    });
    postObject.dataChecks.push({
      objectId: code.objectId,
      checkFlag: checkFlag
    });
  });
  return request({
    url: process.env.VUE_APP_SERVICE_NAME + 'api/sysRolePermission/updateRolePermissions',
    method: 'post',
    data: postObject
  });
}

export function getMenuTree() {
  return request({
    url: process.env.VUE_APP_SERVICE_NAME + 'api/sysMenu/getMenuTree',
    method: 'get',
    params: {
      "operationId": getToken()
    }
  });
}

// 地区架构启用禁用
export function toggleAreaInfo(params) {
  let url = process.env.VUE_APP_SERVICE_NAME + 'api/sysRegMapStruct/create';
  if (params.objectId) {
    url = process.env.VUE_APP_SERVICE_NAME + 'api/sysRegMapStruct/enable';
    return request({
      url: url,
      method: 'get',
      params: params
    });
  } else {
    return request({
      url: url,
      method: 'post',
      data: params
    });
  }
}

// 获取地区架构列表
export function getAreaList(params) {
  let url = process.env.VUE_APP_SERVICE_NAME + 'api/sysRegMapStruct/getListByPage';
  return request({
    url: url,
    method: 'post',
    data: params
  });
}

// 获取地区架构下拉
export function getAreaListDropdown() {
  let url = process.env.VUE_APP_SERVICE_NAME + 'api/sysRegMapStruct/getDropDownList';
  return request({
    url: url,
    method: 'get'
  });
}

// 获取地图设定分页列表
export function getMapSettingList(params) {
  let url = process.env.VUE_APP_SERVICE_NAME + 'api/sysMapCoefficient/getListByPage';
  return request({
    url: url,
    method: 'post',
    data: params
  });
}

// 地图设定
export function setMapParams(params) {
  let url = process.env.VUE_APP_SERVICE_NAME + 'api/sysMapCoefficient/update';
  return request({
    url: url,
    method: 'post',
    data: params
  });
}